<template>
  <div class="p-2">
    <transition :enter-active-class="proxy?.animate.searchAnimate.enter" :leave-active-class="proxy?.animate.searchAnimate.leave">
      <div v-show="showSearch" class="mb-[10px]">
        <el-card shadow="hover">
          <el-form ref="queryFormRef" :model="queryParams" :inline="true">
            <el-form-item label="承&nbsp;&nbsp;租&nbsp;&nbsp;人" prop="name">
              <el-input v-model="queryParams.name" placeholder="请输入承租人" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="到期提醒">
              <el-select>
                <el-option v-for="dict in park_expiration_reminder" :key="dict.value" :label="dict.label" :value="dict.value"> </el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="缴租频次">
              <el-select>
                <el-option v-for="dict in land_frequency_of_rent_payments" :key="dict.value" :label="dict.label" :value="dict.value"> </el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="是否收取">
              <el-select>
                <el-option v-for="dict in is_collect" :key="dict.value" :label="dict.label" :value="dict.value"> </el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="项目名称">
              <el-input v-model="queryParams.propertyName" placeholder="请输入项目名称" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="应收日期">
              <el-date-picker v-model="queryParams.startDate" clearable type="date" value-format="YYYY-MM-DD" placeholder="请选择开始日期">
              </el-date-picker>
              <el-date-picker v-model="queryParams.endDate" clearable type="date" value-format="YYYY-MM-DD" placeholder="请选择结束日期">
              </el-date-picker>
            </el-form-item>
            <el-form-item>
              <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
              <el-button icon="Refresh" @click="resetQuery">重置</el-button>
            </el-form-item>
          </el-form>
        </el-card>
      </div>
    </transition>

    <el-card shadow="never">
      <template #header>
        <div style="display: flex; justify-content: flex-end; align-items: center">
          <el-button type="warning" icon="Download" @click="handleExport">导出 </el-button>
        </div>
      </template>

      <el-table v-loading="loading" :data="hosuerentalList" @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column v-if="false" label="主键标识" align="center" prop="id" />
        <el-table-column label="序号">
          <template #default="scope">
            <span>{{ scope.$index + 1 }}</span>
          </template>
        </el-table-column>
        <el-table-column label="租赁人" align="center" prop="name" />
        <el-table-column label="缴租频次" align="center" prop="frentPaymentFrequency">
          <template #default="scope">
            <dict-tag :options="land_frequency_of_rent_payments" :value="scope.row.frentPaymentFrequency" />
          </template>
        </el-table-column>
        <el-table-column label="联系方式" align="center" prop="contactInformation" />
        <el-table-column label="应收租金(元)" align="center" prop="rentReceivable" />
        <el-table-column label="应收日期" align="center" prop="dueDate">
          <template #default="scope">
            <span>{{ parseTime(scope.row.dueDate, '{y}-{m}-{d}') }}</span>
          </template>
        </el-table-column>
        <el-table-column label="是否收取" align="center" prop="collect">
          <template #default="scope">
            <dict-tag :options="is_collect" :value="scope.row.collect" />
          </template>
        </el-table-column>
        <el-table-column label="实收日期" align="center" prop="paidInTime">
          <template #default="scope">
            <span>{{ parseTime(scope.row.paidInTime, '{y}-{m}-{d}') }}</span>
          </template>
        </el-table-column>
        <el-table-column label="项目名称" align="center" prop="propertyName"></el-table-column>
        <el-table-column label="商户位置" align="center" prop="propertyLocation"></el-table-column>
        <el-table-column label="商户编号" align="center" prop="doorNumber"></el-table-column>
        <el-table-column label="建筑面积(㎡)" align="center" prop="floorArea"></el-table-column>
        <el-table-column label="到期提醒" align="center" prop="expirationReminder">
          <template #default="scope">
            <dict-tag :options="park_expiration_reminder" :value="scope.row.expirationReminder" />
          </template>
        </el-table-column>
        <el-table-column label="操作" width="120" fixed="right" align="center" class-name="small-padding fixed-width">
          <template #default="scope">
            <el-button link type="primary" @click="handleUpdate(scope.row.basicPropertyInformationId, scope.row.id)">编辑</el-button>
            <el-button link type="primary" @click="handleView(scope.row.basicPropertyInformationId, scope.row.id)">详情</el-button>
          </template>
        </el-table-column>
      </el-table>

      <pagination v-show="total > 0" v-model:page="queryParams.pageNum" v-model:limit="queryParams.pageSize" :total="total" @pagination="getList" />
    </el-card>
    <HouseFormDialog ref="houseFormDialogRef" @refresh="getList" />
  </div>
</template>

<script setup name="Hosuerental" lang="ts">
import { listHosuerental, getHosuerental, delHosuerental, addHosuerental, updateHosuerental } from '@/api/zichanguanli/hosuerental/index';
import { HosuerentalVO, HosuerentalQuery, HosuerentalForm } from '@/api/zichanguanli/hosuerental/types';
import HouseFormDialog from '@/components/HouseFormDialog/index.vue';

const { proxy } = getCurrentInstance() as ComponentInternalInstance;
const { park_surrender_tenancy_status, park_frent_payment_frequency, land_frequency_of_rent_payments, park_expiration_reminder, is_collect } =
  toRefs<any>(proxy?.useDict('park_surrender_tenancy_status', 'land_frequency_of_rent_payments', 'park_expiration_reminder', 'is_collect'));

const hosuerentalList = ref<HosuerentalVO[]>([]);
const buttonLoading = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref<Array<string | number>>([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);

const queryFormRef = ref<ElFormInstance>();
const hosuerentalFormRef = ref<ElFormInstance>();

const dialog = reactive<DialogOption>({
  visible: false,
  title: ''
});

const houseFormDialogRef = ref(null);
const houseFormDialogOpen = (type: string, basicPropertyInformationId?: number | string, houseRentalId?: number | string) => {
  houseFormDialogRef.value.open(type, basicPropertyInformationId, houseRentalId);
};

const initFormData: HosuerentalForm = {
  id: null,
  name: undefined,
  associate: undefined,
  contactInformation: undefined,
  leaseholdArea: undefined,
  termContract: undefined,
  startTime: undefined,
  endTime: undefined,
  remainingDays: undefined,
  contractNumber: undefined,
  rentRegulation: undefined,
  appraisedAnnualRent: undefined,
  incrementDate: undefined,
  increasingCountdown: undefined,
  incrementalCriterion: undefined,
  electricityRateStandard: undefined,
  waterTariff: undefined,
  rentOutStatus: undefined,
  rentOutTime: undefined,
  frentPaymentFrequency: undefined,
  equivalentDailyRent: undefined
};
const data = reactive<PageData<HosuerentalForm, HosuerentalQuery>>({
  form: { ...initFormData },
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    collect: undefined,
    endDate: undefined,
    expirationReminder: undefined,
    frentPaymentFrequency: undefined,
    name: undefined,
    propertyName: undefined,
    startDate: undefined
  },
  rules: {
    id: [{ required: true, message: '主键标识不能为空', trigger: 'blur' }],
    name: [{ required: true, message: '承租人不能为空', trigger: 'blur' }],
    associate: [{ required: true, message: '联  系 人不能为空', trigger: 'blur' }],
    contactInformation: [{ required: true, message: '联系方式不能为空', trigger: 'blur' }],
    leaseholdArea: [{ required: true, message: '承租面积不能为空', trigger: 'blur' }],
    termContract: [{ required: true, message: '合同期限不能为空', trigger: 'blur' }],
    startTime: [{ required: true, message: '开始时间不能为空', trigger: 'blur' }],
    endTime: [{ required: true, message: '结束时间不能为空', trigger: 'blur' }],
    remainingDays: [{ required: true, message: '剩余天数不能为空', trigger: 'blur' }],
    contractNumber: [{ required: true, message: '合同编号不能为空', trigger: 'blur' }],
    rentRegulation: [{ required: true, message: '租金规定不能为空', trigger: 'blur' }],
    appraisedAnnualRent: [{ required: true, message: '评估年租金不能为空', trigger: 'blur' }],
    incrementDate: [{ required: true, message: '递增日期不能为空', trigger: 'blur' }],
    increasingCountdown: [{ required: true, message: '递增倒计时不能为空', trigger: 'blur' }],
    incrementalCriterion: [{ required: true, message: '递增标准不能为空', trigger: 'blur' }],
    electricityRateStandard: [{ required: true, message: '电费标准不能为空', trigger: 'blur' }],
    waterTariff: [{ required: true, message: '水费标准不能为空', trigger: 'blur' }],
    rentOutStatus: [{ required: true, message: '退租类型不能为空', trigger: 'change' }],
    rentOutTime: [{ required: true, message: '退租时间不能为空', trigger: 'blur' }]
  }
});

const { queryParams, form, rules } = toRefs(data);

/** 查询房产(其他)租户信息列表 */
const getList = async () => {
  loading.value = true;
  const res = await listHosuerental(queryParams.value);
  hosuerentalList.value = res.rows;
  total.value = res.total;
  loading.value = false;
};

/** 取消按钮 */
const cancel = () => {
  reset();
  dialog.visible = false;
};

/** 表单重置 */
const reset = () => {
  form.value = { ...initFormData };
  hosuerentalFormRef.value?.resetFields();
};

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.value.pageNum = 1;
  getList();
};

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value?.resetFields();
  handleQuery();
};

/** 多选框选中数据 */
const handleSelectionChange = (selection: HosuerentalVO[]) => {
  ids.value = selection.map((item) => item.id);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
};

/** 新增按钮操作 */
const handleAdd = () => {
  houseFormDialogOpen('create');
};

/** 修改按钮操作 */
const handleUpdate = async (basicPropertyInformationId?: number | string, houseRentalId?: number | string) => {
  houseFormDialogOpen('edit', basicPropertyInformationId, houseRentalId);
};

const handleView = async (basicPropertyInformationId?: number | string, houseRentalId?: number | string) => {
  houseFormDialogOpen('view', basicPropertyInformationId, houseRentalId);
};

/** 删除按钮操作 */
const handleDelete = async (row?: HosuerentalVO) => {
  const _ids = row?.id || ids.value;
  await proxy?.$modal.confirm('确定删除所选的信息吗？').finally(() => (loading.value = false));
  await delHosuerental(_ids);
  proxy?.$modal.msgSuccess('删除成功');
  await getList();
};

/** 导出按钮操作 */
const handleExport = () => {
  proxy?.download('zichanguanli/hosuerental/export', { ...queryParams.value }, `hosuerental_${new Date().getTime()}.xlsx`);
};

onMounted(() => {
  getList();
});
</script>
